import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-21
 * Time: 16:59
 */

public class TestDemo2 {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("JavaSE");
        list1.add("JavaWeb");
        list1.add("JavaEE");
        list1.add("JVM");
        list1.add("hello");
        List<String> ret = list1.subList(1,3);
        System.out.println(ret);
        System.out.println(list1);
        System.out.println("===========");
        ret.set(0,"ABC");
        System.out.println(ret);
        System.out.println(list1);
    }

    public static void main4(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("JavaSE");
        list1.add("JavaWeb");
        list1.add("JVM");
        list1.add("JavaEE");
        list1.add("JVM");
        System.out.println(list1);
        System.out.println("==============");
        boolean ret = list1.remove("JVM");
        System.out.println(ret);
        System.out.println(list1);
    }

    public static void main3(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("JavaSE");
        list1.add("JavaWeb");
        list1.add("JavaEE");
        list1.add("JVM");
        System.out.println(list1);
        System.out.println("==============");
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("我是测试list1");
        list2.add("我是测试list2");
        list1.addAll(list2);
        System.out.println(list1);
    }

    public static void main2(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("hello");
        list1.add("JAVA");
        list1.add("ABCD");
       /* Iterator<String> it1 =  list1.iterator();
        while (it1.hasNext()){
            String ret = it1.next();
            if(ret.equals("hello")){
                it1.remove();
            }else {
                System.out.print(ret+" ");
            }
        }
        System.out.println();
        System.out.println("============");*/
        //使用迭代器List相关打印
        ListIterator<String> it2 =  list1.listIterator();
        while (it2.hasNext()) {
            String ret = it2.next();
            if (ret.equals("hello")) {
                it2.add("world");
            } else {
                System.out.print(ret + " ");
            }
        }
        System.out.println();
        System.out.println("==========");
        System.out.println(list1);
    }

    public static void main1(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("hello");
        list1.add("JAVA");
        list1.add("ABCD");
        //直接打印
        System.out.println(list1);
        System.out.println("==============");
        //使用下标+for打印
        for (int i = 0;i<list1.size();i++){
            System.out.print(list1.get(i)+" ");
        }
        System.out.println();
        System.out.println("==============");
        //使用foreach打印
        for (String s:list1) {
            System.out.print(s+" ");
        }
        System.out.println();
        System.out.println("==============");
        //使用迭代器打印
       Iterator<String> it1 =  list1.iterator();
       while (it1.hasNext()){
           System.out.print(it1.next()+" ");
       }
        System.out.println();
        System.out.println("==============");
       //使用迭代器List相关打印
        ListIterator<String> it2 =  list1.listIterator();
        while (it2.hasNext()){
            System.out.print(it2.next()+" ");
        }
        System.out.println();
    }
}
